﻿using System;
using System.Data;
using System.Configuration;

using System.Collections ;
using System.Collections.Specialized ;

using Common ;

using Castle.Core.Interceptor;
using Castle.DynamicProxy;

namespace com.globalwave.system
{
    /// <summary>
    /// Dict 的摘要说明
    /// </summary>
    public class DictService : BaseService
    {
        private static NameValueCollection whereMap = null ;
        public DictService()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        virtual public DataSet query(
                NameValueCollection dictCriterion, 
                int pageIndex, 
                int pageSize) 
        {
            string sql = "SELECT UserCode AS dict_id, 'subsite' AS domain_, cast(_id as nvarchar(36))+'' AS code_, FullName AS name_ FROM dbo.Pub_SubSite where usercode != ''";

            return dao.query(sql, pageIndex, pageSize, dictCriterion, " order by dict_id ", "Dict");
        }


        public bool checkDictExisted(NameValueCollection dict) {
            NameValueCollection dictCriterion = new NameValueCollection() ;
            dictCriterion.Add("dict_id", dict.Get("dict_id")) ;

            return dao.count("system_dict",dictCriterion) > 0 ;
            
        }

        virtual public int create(NameValueCollection dict)
        {
            return dao.insertTable("system_dict",dict) ;
        }

        virtual public int update(NameValueCollection dict, NameValueCollection criterion)
        {    
            return dao.updateTable("system_dict",dict, criterion) ;    
        }

        virtual public int delete(NameValueCollection criterion)
        {
            
            if (criterion.Count == 0 ) {
                throw new BaseException("E0000002","Param can't be null!!") ;
            }

            return dao.deleteTable("system_dict", criterion) ;
            
        }

        virtual public int deleteAll(string[] dictIds)
        {

            NameValueCollection criterion = new NameValueCollection();
            foreach (string dictId in dictIds)
            {
                criterion.Add("dict_ids", dictId);
            }

            if (criterion.Count == 0)
            {
                throw new BaseException("E0000002", "Param can't be null!!");
            }

            return dao.deleteTable("system_dict", criterion);
            
        }

        /*
        public NameValueCollection get(string dict_id)
        {
            NameValueCollection criterion = new NameValueCollection();
            criterion.Add("dict_id", dict_id);

            return dao.queryTable("system_dict", criterion);
        }*/

        
        public NameValueCollection getWhereConfigMap() {
            if (whereMap != null) {
                return whereMap;
            }

            whereMap = new NameValueCollection();

            whereMap.Add("dict_ids", " dict_id in (@dict_ids) ");

            return whereMap;
        }


        public static DictService getService()
        {

            ProxyGenerator generator = new ProxyGenerator();

            DictService service = generator.CreateClassProxy<DictService>(new TransactionInterceptor());

            return service;
        }
    }
}